package com.jalonjia.seckill.core.xxljob;

import com.jalonjia.seckill.core.entity.SeckillActivity;
import com.jalonjia.seckill.core.service.SeckillActivityService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class JobHandler {
	private final SeckillActivityService seckillActivityService;

	/**
	 * 秒杀活动预热任务
	 * 每分钟执行一次
	 */
	@XxlJob("seckillWarmupJobHandler")
	public void seckillWarmupJobHandler() {
		LocalDateTime now = LocalDateTime.now();
		LocalDateTime minTime = now.plus(0, ChronoUnit.MINUTES);
		LocalDateTime maxTime = now.plus(1, ChronoUnit.HOURS);
		seckillActivityService.warmupActivity(seckillActivityService.query()
				.gt("start_time", minTime)
				.lt("start_time", maxTime)
				.list());
		XxlJobHelper.handleSuccess();
	}
}